import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedHashMap;

import junit.framework.TestCase;

/**
 * 
 * @author Seth Mielke, Jonathan Hsu
 * 
 * @modified 8/6/11
 * 
 */
public class OpTest extends TestCase {
	private FileReader pin;
	private FileWriter pout;
	private Word[] m;
	private Word[] r;
	LinkedHashMap<String, Word> traceTable;
	public OpTest(String name) {
		super(name);
	}

	protected void setUp() throws Exception {
		super.setUp();
		pin = new FileReader("test.txt");
		pout = new FileWriter("test.txt");
		m = new Word[256];
		for (int i = 0; i < 255; i++) {
			m[i] = new Word();
		}
		m[0].set(1);
		m[1].set(4);
		r = new Word[4];
		r[0] = new Word(15);
		r[2] = new Word();
		traceTable = new LinkedHashMap<String, Word>();

	}
	public void testAdd() throws IOException {
		OpAdd op = new OpAdd(r,m,pin,pout);
		op.exec(0, traceTable);
		assertEquals("Add didn't work for basic test case",19,r[0].get());
	}

	public void testLoad() throws IOException {
		OpLoad op = new OpLoad(r,m,pin,pout);
		op.exec(0, traceTable);
		assertEquals("Load didn't work for basic test case",4,r[0].get());
	}
	public void testLoadI() throws IOException {
		OpLoadI op = new OpLoadI(r,m,pin,pout);
		op.exec(0, traceTable);
		assertEquals("LoadI didn't work for basic test case",1,r[0].get());
	}
	public void testStore() throws IOException {
		OpStore op = new OpStore(r,m,pin,pout);
		op.exec(0, traceTable);
		assertEquals("Store didn't work for basic test case",15,m[0].get());
	}
	public void testSub() throws IOException {
		OpSub op = new OpSub(r,m,pin,pout);
		op.exec(0, traceTable);
		assertEquals("Sub didn't work for basic test case",11,r[0].get());
	}
	public void testMult() throws IOException {
		OpMultiply op = new OpMultiply(r,m,pin,pout);
		op.exec(0, traceTable);
		assertEquals("Multiply didn't work for basic test case",60,r[0].get());
	}

	public void testDivide() throws IOException {
		OpDivide op = new OpDivide(r,m,pin,pout);
		op.exec(0, traceTable);
		assertEquals("Divide didn't work for basic test case",3,r[0].get());
	}
	public void testOr() throws IOException {
		OpOr op = new OpOr(r,m,pin,pout);
		op.exec(0, traceTable);
		assertEquals("Or didn't work for basic test case",15,r[0].get());
	}

	public void testAnd() throws IOException {
		OpAnd op = new OpAnd(r,m,pin,pout);
		op.exec(0, traceTable);
		assertEquals("And didn't work for basic test case",1,r[0].get());
	}

	public void testSHL() throws IOException {
		OpSHL op = new OpSHL(r,m,pin,pout);
		op.exec(0, traceTable);
		assertEquals("SHL didn't work for basic test case",30,r[0].get());
	}

	public void testSHR() throws IOException {
		OpSHR op = new OpSHR(r,m,pin,pout);
		op.exec(0, traceTable);
		assertEquals("SHR didn't work for basic test case",7,r[0].get());
	}
	public void testBR() throws IOException {
		//02000
		m[0].set(8192);
		OpBR op = new OpBR(r,m,pin,pout);
		int PC = op.exec(0, traceTable);
		assertEquals("BR didn't work for basic test case",0,PC);
	}	
	public void testBRZ() throws IOException {
		OpBRZ op = new OpBRZ(r,m,pin,pout);
		int PC = op.exec(0, traceTable);
		assertEquals("BRZ didn't work for basic test case",1 ,PC);
	}

	public void testBRN() throws IOException {
		OpBRN op = new OpBRN(r,m,pin,pout);
		r[0].set(-1);
		m[0].set(10);
		int PC = op.exec(0, traceTable);
		assertEquals("BRN didn't work for basic test case",10,PC);
	}
	public void testBRS() throws IOException {
		OpBRS op = new OpBRS(r,m,pin,pout);
		m[0].set(5);
		int PC = op.exec(0, traceTable);
		assertEquals("BRS didn't work for basic test case",5,PC);
	}
}
